package com.byedu.utils;

import com.alibaba.fastjson.JSONObject;
import com.byedu.entity.ApiUserInfo;
import com.byedu.entity.Course;
import com.byedu.utils.http.HttpRequestUtil;
import org.apache.log4j.Logger;

import java.io.UnsupportedEncodingException;

public class RectUrl {

    private static Logger logger = Logger.getLogger(RectUrl.class);

    public static boolean ISTEST = false;


    public static String CALLBAKEURL = "cb.by-edu.com";
    public static String APIURL = "bay-api.by-edu.com";
//    public static String APIURL="manage.by-edu.com";

    public static String AUTHORIZEURLRELOGIN = "http://" + CALLBAKEURL + "/reloginServlet";// 目标访问地址
    public static String AUTHORIZEURLMAIN = "http://" + CALLBAKEURL + "/mainServlet";// 目标访问地址
    public static String AUTHORIZEURLLogin = "http://" + CALLBAKEURL + "/loginServlet";// 目标访问地址

    public static String PAYURL = "http://" + CALLBAKEURL + "/createOrder";

    //主页面地址
    public static String LEARNURL = "http://learn.by-edu.com/";
    public static String CALLBACKURL = "http://" + APIURL + "/login/wx";


    public static String GETCOURCE = "http://" + APIURL + "/course/";
    public static String GETUSERBYOPENID = "http://" + APIURL + "/user/getuserbyopenid/";
    public static String GETUSERBYUSERID = "http://" + APIURL + "/user/getuserbyuid/";
    public static String ADDUSERURL = "http://" + APIURL + "/usercourse/addUserCourse";

    public static String NOTICEFYURL = "http://" + CALLBAKEURL + "/notifyServlet";


    public final static String WeixinLoginUrl = "http://cb.by-edu.com/loginServlet";

    public final static String phoneLoginUrl = "http://learn.by-edu.com/";

    public final static String MainUrl = "http://learn.by-edu.com";


//    public static String sendRedirect(ApiUserInfo weiXinUserVO) throws UnsupportedEncodingException {
//        String baseStr = "";
//        if(!StringUtil.isNullOrEmpty(weiXinUserVO.getCellPhone())){
//            baseStr = MainUrl+"?userId="+weiXinUserVO.getUserId()+"&openId="+weiXinUserVO.getOpenId()+"&phone="+weiXinUserVO.getCellPhone();
//            if(!StringUtil.isNullOrEmpty(weiXinUserVO.getNickname())){
//                String name = new String(weiXinUserVO.getNickname().getBytes("utf-8"),"iso-8859-1");
//                baseStr = baseStr +"&name="+name;
//            }
//            if(!StringUtil.isNullOrEmpty(weiXinUserVO.getHeadImgUrl())){
//                baseStr = baseStr +"&imgUrl="+weiXinUserVO.getHeadImgUrl();
//            }
//        }else{
//            baseStr = phoneLoginUrl+"?userId="+weiXinUserVO.getUserId()+"&openId="+weiXinUserVO.getOpenId();
//            if(!StringUtil.isNullOrEmpty(weiXinUserVO.getNickname())){
//                String name = new String(weiXinUserVO.getNickname().getBytes("utf-8"),"iso-8859-1");
//                baseStr = baseStr +"&name="+name;
//            }
//            if(!StringUtil.isNullOrEmpty(weiXinUserVO.getHeadImgUrl())){
//                baseStr = baseStr +"&imgUrl="+weiXinUserVO.getHeadImgUrl();
//            }
//            baseStr = baseStr+"#/login";
//        }
//
//        return baseStr;
//    }


    public static String sendRedirect(ApiUserInfo weiXinUserVO) throws UnsupportedEncodingException {
        String baseStr = MainUrl + "?openId=" + weiXinUserVO.getOpenId();
//        if(!StringUtil.isNullOrEmpty(weiXinUserVO.getCellPhone())){
//            baseStr = MainUrl+"?userId="+weiXinUserVO.getUserId()+"&openId="+weiXinUserVO.getOpenId()+"&phone="+weiXinUserVO.getCellPhone();
//            if(!StringUtil.isNullOrEmpty(weiXinUserVO.getNickname())){
//                String name = new String(weiXinUserVO.getNickname().getBytes("utf-8"),"iso-8859-1");
//                baseStr = baseStr +"&name="+name;
//            }
//            if(!StringUtil.isNullOrEmpty(weiXinUserVO.getHeadImgUrl())){
//                baseStr = baseStr +"&imgUrl="+weiXinUserVO.getHeadImgUrl();
//            }
//        }else{
//            baseStr = phoneLoginUrl+"?userId="+weiXinUserVO.getUserId()+"&openId="+weiXinUserVO.getOpenId();
//            if(!StringUtil.isNullOrEmpty(weiXinUserVO.getNickname())){
//                String name = new String(weiXinUserVO.getNickname().getBytes("utf-8"),"iso-8859-1");
//                baseStr = baseStr +"&name="+name;
//            }
//            if(!StringUtil.isNullOrEmpty(weiXinUserVO.getHeadImgUrl())){
//                baseStr = baseStr +"&imgUrl="+weiXinUserVO.getHeadImgUrl();
//            }
//            baseStr = baseStr+"#/login";
//        }

        return baseStr;
    }

    public static void main(String[] args) {

        JSONObject jsonPara = new JSONObject();
        jsonPara.put("openId", "oMH9vwGIU7rGneHe4tz8aakjaLLY");
        ApiUserInfo apiUserInfo = RectUrl.getApiUserInfo(jsonPara);
        System.out.println(apiUserInfo.toString());
    }


    /**
     * @param courseId
     * @return course
     */
    public static Course getCourseId(String courseId, String userId) {
        Course course = null;
        String url = GETCOURCE + courseId + "/" + userId;
        try {
            String userString = HttpRequestUtil.sendGet(url, null);// 拿去返回值
            JSONObject jsonUser = JSONObject.parseObject(userString);

            if (!jsonUser.getBoolean("success")) {
                return null;
            }
            course = JSONObject.parseObject(jsonUser.getString("data"), Course.class);

        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }

        return course;
    }


    /**
     * 通过userId 或 openId 获取用户信息
     *
     * @param userId
     * @param openId
     * @return myApiUserInfo
     */
    public static ApiUserInfo getUserbyId(String userId, String openId) {
        System.out.println("userId = " + userId + ", openId = " + openId);
        ApiUserInfo myApiUserInfo = null;
        String url = null;
        try {
            if (userId != null) {
                url = GETUSERBYUSERID + userId;
            } else if (openId != null) {
                url = GETUSERBYOPENID + openId;

            } else {
                return null;
            }
            System.out.println(url);
            String userString = HttpRequestUtil.sendGet(url, null);// 拿去返回值
            System.out.println("***********************");
            System.out.println(userString);
            System.out.println("***********************");
            JSONObject jsonUser = JSONObject.parseObject(userString);
            System.out.println("---------------------------");
            System.out.println(jsonUser);
            System.out.println("---------------------------");
            if (!jsonUser.getBoolean("success")) {
                return null;
            }
            myApiUserInfo = JSONObject.parseObject(jsonUser.getString("data"), ApiUserInfo.class);

        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }

        return myApiUserInfo;
    }

//    public static String getLeanUrl(ApiUserInfo myApiUserInfo){
//        return RectUrl.LEARNURL + "?userId=" + myApiUserInfo.getUserId() + "&openId="+myApiUserInfo.getOpenId();
//
//    }


    public static ApiUserInfo getApiUserInfo(JSONObject jsonPara) {
        ApiUserInfo myApiUserInfo = null;
        try {

            String userString = HttpRequestUtil.sendPost(RectUrl.CALLBACKURL, jsonPara);// 拿去返回值
            JSONObject jsonUser = JSONObject.parseObject(userString);

            if (!jsonUser.getBoolean("success")) {
                return null;
            }
            myApiUserInfo = JSONObject.parseObject(jsonUser.getString("data"), ApiUserInfo.class);

        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }

        return myApiUserInfo;

    }

    public static boolean getpayCource(String userId, String courseId) throws Exception {

        String userString = HttpRequestUtil.sendGet("http://" + APIURL + "/usercourse/" + userId + "/" + courseId, null);// 拿去返回值
        JSONObject jsonUser = JSONObject.parseObject(userString);
        return jsonUser.getBoolean("data");
    }
}
